//
//  MessageFrame.m
//  QuanZiChat
//
//  Created by lanou3g on 15/11/25.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "MessageFrame.h"
#import "MessageInfo.h"
#import "NSString+MESstringNet.h"

@implementation MessageFrame

#pragma mark ========== cell自适应高度
-(void)setMessageinfo:(MessageInfo *)messageinfo{
    _messageinfo = messageinfo;
    //计算每个控件的frame和行高
    //获取屏幕的宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    //设置一个统一的间距
    CGFloat margin = 5;
    
    //计算时间lable的frame
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = screenW;
    CGFloat timeH = 15;
    if(!messageinfo.hideTime){
        _timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
    }
    
    
    //计算头像的frame
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    CGFloat iconX = messageinfo.type == MessageYou ? margin : screenW - margin - iconW;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    //计算正文的frame
    //1.计算正文的大小
    CGSize textSize = [messageinfo.text sizeOfTextWithMaxsie:CGSizeMake(200, MAXFLOAT) font:[UIFont systemFontOfSize:14]];
    CGFloat textH = textSize.height + 10;
    CGFloat textW = textSize.width + 12;
    //2.计算正文的X,Y
    CGFloat textY = iconY;
    CGFloat textX = messageinfo.type == MessageYou ? CGRectGetMidX(_iconFrame) + 15:(screenW - margin - iconW - textW);
    _textFrame = CGRectMake(textX, textY, textW, textH);
    
    
    //计算行高
    //比较头像和正文的最大的Y值,用最大的Y值+margin
    //MAX 一个定义的宏 取两者最大值
    CGFloat maxY = MAX(CGRectGetMaxY(_textFrame), CGRectGetMaxY(_iconFrame));
    _rowhight = maxY;
}



@end
